package com.example.vv.res.net;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.vv.fragmentWork.utlis.ShareUtils;
import com.example.vv.fragmentWork.utlis.UserInfo;
import com.example.vv.req.BaseRequest;
import com.example.vv.req.SearchRequest;

import java.io.IOException;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitUtils {
    private static RequestInterface requestInterface;
    static {
        requestInterface = new Retrofit.Builder()
                .baseUrl(ConfigCode.IP)
                .client(new OkHttpClient.Builder().build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
                .create(RequestInterface.class);
    }
    public static Observable<BaseResponse> request(String API, BaseRequest baseRequest){
        baseRequest.initMap();
        UserInfo userInfo = ShareUtils.userInfo;
        if (userInfo!=null) {
            baseRequest.map.put("userId",ShareUtils.userInfo.getPhone());
        }
        return requestInterface.POST_API(API,baseRequest.map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Function<ResponseBody, BaseResponse>() {
                    @Override
                    public BaseResponse apply(ResponseBody responseBody) throws Exception {
                        JSONObject jb = null;
                        try {
                            jb = JSON.parseObject(responseBody.string());
                            String errorCode = jb.getString("errorCode");
                            String errorMsg = jb.getString("errorMsg");
                            String data = jb.getString("data");
                            BaseResponse response = new BaseResponse();
                            response.setData(data);
                            response.setErrorCode(errorCode);
                            response.setErrorMsg(errorMsg);
                            return response;

                        }catch (IOException e){
                            e.printStackTrace();
                            return null;
                        }
                    }
                });
    }

    /**
     * GET请求
     * @param api
     * @return
     * 这是上游发布的数据
     *
     */
    public static Observable<BaseResponse> request(String api){
        return requestInterface.GET_API(api)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Function<ResponseBody, BaseResponse>() {
                    @Override
                    public BaseResponse apply(ResponseBody responseBody) throws Exception {
                        JSONObject obj = null;
                        try {
                            obj = JSON.parseObject(responseBody.string());
                            String errorCode = obj.getString("errorCode");
                            String errorMsg = obj.getString("errorMsg");
                            String data = obj.getString("data");
                            BaseResponse response = new BaseResponse();
                            response.setErrorCode(errorCode);
                            response.setErrorMsg(errorMsg);
                            response.setData(data);
                            return response;
                        } catch (IOException e) {
                            e.printStackTrace();
                            return null;
                        }
                    }
                });
    }

    public static Observable<BaseResponse> searchQuest(String API, String value){
        SearchRequest searchRequest = new SearchRequest(value);
        searchRequest.initMap();
        return requestInterface.POST_API(API,searchRequest.map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Function<ResponseBody, BaseResponse>() {
                    @Override
                    public BaseResponse apply(ResponseBody responseBody) throws Exception {
                        JSONObject jb = null;
                        try {
                            jb = JSON.parseObject(responseBody.string());
                            String errorCode = jb.getString("errorCode");
                            String errorMsg = jb.getString("errorMsg");
                            String data = jb.getString("data");
                            BaseResponse response = new BaseResponse();
                            response.setData(data);
                            response.setErrorCode(errorCode);
                            response.setErrorMsg(errorMsg);
                            return response;

                        }catch (IOException e){
                            e.printStackTrace();
                            return null;
                        }
                    }
                });
    }

    public static Observable<BaseResponse> hotRequest(String API){
        return requestInterface.GET_API(API)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Function<ResponseBody, BaseResponse>() {
                    @Override
                    public BaseResponse apply(ResponseBody responseBody) throws Exception {
                        JSONObject jb = null;
                        try {
                            jb = JSON.parseObject(responseBody.string());
                            String errorCode = jb.getString("errorCode");
                            String errorMsg = jb.getString("errorMsg");
                            String data = jb.getString("data");
                            BaseResponse response = new BaseResponse();
                            response.setData(data);
                            response.setErrorCode(errorCode);
                            response.setErrorMsg(errorMsg);
                            return response;

                        }catch (IOException e){
                            e.printStackTrace();
                            return null;
                        }
                    }
                });
    }
}
